﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace VitCode.ObjectResolutionHelper
{
    /// <summary>
    /// 一个协助分析Class的附助类
    /// </summary>
    public class LookUpClassHelper
    {
        /// <summary>
        /// 获取类集体
        /// </summary>
        /// <param name="strAssName">指定程序集名称</param>
        /// <param name="strNaming">指定命名空间下[必须是程序集中存在的]</param>
        /// <returns></returns>
        public static ArrayList GetTypes(string strAssName, string strNaming)
        {
            Assembly assObj = Assembly.Load(strAssName);
            var al = new ArrayList();
            Type[] typeObjs = assObj.GetTypes();
            foreach (Type t in typeObjs)
            {
                if (t.Namespace == strNaming)
                    al.Add(t);
            }
            return al;
        }

        /// <summary>
        /// 获取一个类的所有属性
        /// </summary>
        /// <param name="objType">对象类型</param>
        /// <returns>属性集合</returns>
        public static PropertyInfo[] GetPros(Type objType)
        {
            PropertyInfo[] p = objType.GetProperties();
            return p;
        }

        /// <summary>
        /// 获取对象名上的OrMemo描述,通常是类在UI的呈现名称,如Site类,通常被呈现为[地点]
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <returns>对象是UI上描述</returns>
        public static string GetOrMemoDesValue(Type type)
        {
            string reVal = "";

            if (type != null)
            {
                var desAttrObj =
                    ((DescriptionAttribute) Attribute.GetCustomAttribute(type, typeof (DescriptionAttribute)));
                if (desAttrObj != null)
                    reVal = desAttrObj.Description;
            }
            return reVal;
        }

        /// <summary>
        /// 获取属性上的OrMemo描述,通常是属性在UI的呈现名称,如Address类,通常被呈现为[居住地址]
        /// </summary>
        /// <param name="proInfo">属性</param>
        /// <returns>属性是UI上的描述</returns>
        public static string GetOrMemoDesValue(PropertyInfo proInfo)
        {
            return GetOrDesVal(proInfo, typeof (OrMemoAttribute));
        }

        /// <summary>
        /// 获取对象名上的自定义描述
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="attrType">描述的类型(UI呈现描述,对象某个配置属性描述)</param>
        /// <returns>获取对象名上的自定义描述</returns>
        public static string GetOrDesVal(Type type, Type attrType)
        {
            string reVal = "";

            if (type != null)
            {
                Attribute desAttrObj = Attribute.GetCustomAttribute(type, attrType);
                if (desAttrObj != null)
                {
                    PropertyInfo pro = attrType.GetProperty("Description");
                    if (pro != null)
                        reVal = pro.GetValue(desAttrObj, null).ToString();
                }
            }
            return reVal;
        }
        /// <summary>
        /// 获取属性名上的自定义描述
        /// </summary>
        /// <param name="proInfo">对象属性</param>
        /// <param name="attrType">描述的类型(UI呈现描述,对象某个配置属性描述)</param>
        /// <returns>获取属性名上的自定义描述</returns>
        public static string GetOrDesVal(PropertyInfo proInfo, Type attrType)
        {
            string reVal = "";

            if (proInfo != null)
            {
                Attribute desAttrObj = Attribute.GetCustomAttribute(proInfo, attrType);
                if (desAttrObj != null)
                {
                    PropertyInfo pro = attrType.GetProperty("Description");
                    if (pro != null)
                        reVal = pro.GetValue(desAttrObj, null).ToString();
                }
            }
            return reVal;
        }

        /// <summary>
        /// 把项目名称中的Core替换成History,从而生成Core项目相应的History项目名称
        /// </summary>
        /// <param name="strOrg">需要替换的字符</param>
        /// <returns>Core项目相应的History项目名称</returns>
        public static string ReplaceHistoryWord(string strOrg)
        {
            return strOrg.Replace(".Core", ".History");
        }

        /// <summary>
        /// 通过对象类型获取所属程序集名称
        /// </summary>
        /// <param name="t">对象类型</param>
        /// <returns>类型所属程序集名称</returns>
        public static string GetAssName(Type t)
        {
            return t.Assembly.FullName.Substring(0, t.Assembly.FullName.IndexOf(','));
        }

        /// <summary>
        /// 通过Core项目中的业务对象获取相应的历史记录对象类型
        /// 因为
        /// </summary>
        /// <param name="baseObj">业务对象类型</param>
        /// <returns>相应有历史记录对象类型</returns>
        public static Type GetHisType(Type baseObj)
        {
            string assName = GetAssName(baseObj);
            string hisAssName = ReplaceHistoryWord(assName);
            //业务对象与相应的历史记录对象在命名上的一定的规则,如业务对象[Site]对应的历史记录对象为[Site_Record]
            string hisTypeFullName = ReplaceHistoryWord(baseObj.ToString()) + HistoryKeyInfo.LAST_STRING;
            Assembly assObj = Assembly.Load(hisAssName);
            Type reType = assObj.GetType(hisTypeFullName, false);
            return reType;
        }

        /// <summary>
        /// 把带逗号分隔符的字符串转换成一个字符串集合
        /// </summary>
        /// <param name="sc">把带逗号分隔符的字符串</param>
        /// <returns>字符串集合</returns>
        public static string ConvertStringValue(StringCollection sc)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < sc.Count; i++)
            {
                if (sb.Length > 0)
                    sb.Append(",");
                sb.Append(sc[i]);
            }
            return sb.ToString();
        }
    }
}